// basicnpc.txt
// A very simple, naive text. Creature attacks anything it hates nearby.
// Once it has won, it returns to its home.
// Memory Cells:
//   Cell 0 - If 0, default behavior. If 1, it doesnt fidget.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
//   Cell 3 - Number of state when talked to. Plays default text if 0.

begincreaturescript;

variables;

short i,target;
short going = 0;
short reached_pt = 0;
short dest_pt;

body;

beginstate INIT_STATE;
	set_level(ME,5);
	change_max_health(ME,40);
	set_strategy(ME,10 + get_strategy(ME));

	if ((get_memory_cell(0) == 0) || (get_memory_cell(0) == 1)) {
		dest_pt = 1;
		}
		else dest_pt = 2;

	break;

beginstate DEAD_STATE;
	inc_flag(5,7,1);
break;

beginstate START_STATE; 
	if ((get_memory_cell(0) == 0) || (get_memory_cell(0) == 1)) {
		if (gf(5,10) == 1)
			going = 1;
			else going = 0;
			
		}
	if ((get_memory_cell(0) == 2) || (get_memory_cell(0) == 3)) {
		if (gf(5,11) == 1)
			going = 1;
			else going = 0;
			
		}
		
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_foe_target(ME,-1);
		}
	
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	
	if ((get_memory_cell(0) == 0) || (get_memory_cell(0) == 1)) {
		if ((going) && (gf(5,8) > 0)) {
			reached_pt = 1;
			approach_nav_point(ME,3,2);
			end();
			}
		}
		
	if ((going) && (reached_pt == 0)) {
		approach_nav_point(ME,dest_pt,2);
		if (dist_to_nav_point(ME,dest_pt) <= 2) 
			reached_pt = 1;
		}
	if ((going) && (reached_pt > 0) && (gf(7,26) != 1) && (gf(7,28) != 1)) 
		fidget(ME,15);


	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);
	
	if ((get_memory_cell(0) == 0) || (get_memory_cell(0) == 1)) {
		if ((char_ok(67)) && (can_see_char(67))) {
			set_foe_target(ME,67);
			}
		if ((char_ok(67) == FALSE) && (char_ok(68)) && (can_see_char(68))) {
			set_foe_target(ME,68);
			}
		if ((char_ok(68) == FALSE) && (char_ok(69)) && (can_see_char(69))) {
			set_foe_target(ME,69);
			}
		if ((char_ok(69) == FALSE) && (char_ok(70)) && (can_see_char(70))) {
			set_foe_target(ME,70);
			}
		}
	if ((get_memory_cell(0) == 2) || (get_memory_cell(0) == 3)) {
		if ((char_ok(72)) && (can_see_char(72))) 
			set_foe_target(ME,72);
		else if ((char_ok(73)) && (can_see_char(73))) 
			set_foe_target(ME,73);
		else if ((char_ok(74)) && (can_see_char(74))) 
			set_foe_target(ME,74);
		else if ((char_ok(75)) && (can_see_char(75))) 
			set_foe_target(ME,75);
		else if ((char_ok(76)) && (can_see_char(76))) 
			set_foe_target(ME,76);
		else if ((char_ok(77)) && (can_see_char(77))) 
			set_foe_target(ME,77);
		else if ((char_ok(78)) && (can_see_char(78))) 
			set_foe_target(ME,78);
		else if ((char_ok(79)) && (can_see_char(79))) 
			set_foe_target(ME,79);
		}
		
	do_attack();
break;

beginstate TALKING_STATE;
	if (going) {
		print_str("Talking: This soldier is too busy to talk right now.");
		}
		else begin_talk_mode(8);
break;